

//2014.1.18 һ(ȶԹ)
//ע: ڵƬ͵ǰֽҪЧ, Ϊʱ
//ᶪʧ. ʧĻһǶʧһֽ, Ϊ˱, ͷ.

//Ŀǰ Connect жнط

//2014.1.19 ˵Զ, ƻѿؼIDΪ, ԹһԶ

link VT code {} = #.Cloud.code;
link unit CHIP {} = #.MCU;

public uint16 max_length = #const 20;

//public uint32 baud = #const 9600;
//public uint32 baud = #const 38400;
public uint32 baud = #const 115200;

public [uint8*max_length] buffer;
uint8 length;

public [->uint8 * 32] FlagList;
bool DataIsOK;
bool EventIsOK;
bool Connected;
uint8 ErrNumber;
uint8 DataLength;

public bool isBusy;
public bool isNeed;


//--Զڴģ
VT linka
{
	type = [uint16] uint32;
}
void set_uint32( uint16 addr, uint32 data )
{
	#.linker.SetValue( addr.8(uint8), addr.0(uint8), data );
}
uint32 get_uint32( uint16 addr )
{
	return #.linker.GetValue( addr.8(uint8), 0x80 + addr.0(uint8) );
}
//----------------------------------------------------
//ڳʼ,:9600,8λ,1ֹͣλ,ûżУ
public void OS_init()
{
	RDE_DIR = 1;
	RDE_OUT = 0;
	
	RXD_DIR = 0;
	RXD_OUT = 1;
	
	TXD_DIR = 1;
	TXD_OUT = 1;
	
	//7 = 1: շͱ־
	//5 = 1: ;
	CHIP.UCSRA = 0b0110_0000;
	//7 = 1: ʹܷж
	//4 = 1: 
	//3 = 1: 
	CHIP.UCSRB = 0b1001_1000;
	CHIP.UCSRC = 0x86;
	
	//Baud = (Fosc/16) / (UBRR + 1)
	//UBRR = (Fosc/16) / Boud - 1
	//Fosc = 11.0592MHz
	//Baud = 9600
	//CHIP.UBRRH = 0;
	//CHIP.UBRRL = 71;
	uint16 UBRR = 6912 / (uint16)(baud / 100) - 1;
	CHIP.UBRRH = UBRR.8(uint8);
	CHIP.UBRRL = UBRR.0(uint8);
	
	length = 0;
	
	ErrNumber = 0;
	DataLength = 0;
	DataIsOK = false;
	EventIsOK = false;
	Connected = false;
	
	isBusy = false;
	isNeed = false;
}
//----------------------------------------------------
//ݽж
interrupt [CHIP.WATCH.usart_RXC]
void RXC()
{
	decode();
}
//---------------------------------------------------
//Э
public void decode()
{
	//жϳжȡ UDR,˳½ж
	buffer[length] = CHIP.UDR;
	length + 1;
	
	//+++++++++++++++++++++++
	//ͨЭ 0x55 N * * * ... * * * SUM
	uint8 L = length;
	uint8 Head = buffer[0];
	
	//жЭͷ
	if( L == 1 && Head != 0x55 ) {
		length = 0;
		//WriteByte( 0x55 );
		return;
	}
	if( L == 2 ) {
		DataLength = buffer[1];
		return;
	}
	if( L < DataLength + 3 ) return;
	uint8 CMD = buffer[2];
	uint8 SUM = buffer[L - 1];
	
	length = 0;
	
	//ͨЭ
	//+++++++++++++++++++++++
	
	//ͨű־
	if( DataLength == 0 ) {
		StartWrite();
		
		//ǰֽ
		SendNullData();
		
		if( Connected ) {
			WriteByte( 0x55 );
		}
		else {
			WriteByte( 0xAA );
		}
		EndWrite();
		
		//Ѿ, Źȴλ
		if( Connected ) {
			#asm "CLI"
			CHIP.WDTCR = 0b0000_1000;
			repeat {}
		}
		Connected = true;
		return;
	}
	//GUIЭ, Ϊ¼ͨ
	if( CMD == 0 ) {
		DataIsOK = true;
		return;
	}
	if( CMD == 1 ) {
		uint8 ID = buffer[3];
		uint8 EI = buffer[4];
		FlagList[ID] = FlagList[ID] | EI;
		EventIsOK = true;
		return;
	}
	if( CMD == 2 ) {
		EventIsOK = true;
		return;
	}
}
//---------------------------------------------------
//
public void Connect()
{
	//ϵͳ
	StartWrite();
	SendNullData();
	WriteByte( 0x55 );
	WriteByte( 0x02 );
	WriteByte( 0x00 );
	WriteByte( 0x00 );
	WriteByte( 0x00 );
	EndWrite();
	
	repeat {
		repeat until CHIP.UCSRA.7(bit) == 1; {}
		decode();
		if( Connected ) return;
	}
}
//---------------------------------------------------
//ȡ¼־
public void GetEventFlag()
{
	return;
	
	repeat until !isNeed; {}
	
	isBusy = true;
	
	StartWrite();
	WriteByte( 0x55 );
	WriteByte( 6 );
	WriteByte( 0 );
	WriteByte( 3 );
	WriteByte( 0 );
	WriteByte( 0 );
	WriteByte( 0 );
	WriteByte( 0 );
	WriteByte( 0 );
	EndWrite();
	repeat until EventIsOK; {}
	EventIsOK = false;
	
	isBusy = false;
}
//---------------------------------------------------
//һַ
public void SetString( uint8 ID, uint8 Command, [code uint16*?] string )
{
	isBusy = true;
	
	StartWrite();
	WriteByte( 0xAA );
	WriteByte( 0 );
	WriteByte( ID ); //ָIDĿؼ
	WriteByte( Command ); //
	repeat first int16 i = 0; each i + 1; {
		if( string[ i ] == 0 ) {
			break;
		}
		uint16 char = string[ i ];
		WriteByte( char.0(uint8) );
		WriteByte( char.8(uint8) );
	}
	WriteByte( 0 );
	WriteByte( 0 );
	EndWrite();
	
	isBusy = false;
}
//---------------------------------------------------
//һ
public void SetValue( uint8 ID, uint8 Command, uint32 data )
{
	isBusy = true;
	
	StartWrite();
	WriteByte( 0x55 );
	WriteByte( 6 );
	WriteByte( ID ); //ָIDĿؼ
	WriteByte( Command ); //
	WriteByte( data.0(uint8) );
	WriteByte( data.8(uint8) );
	WriteByte( data.16(uint8) );
	WriteByte( data.24(uint8) );
	WriteByte( 0 );
	EndWrite();
	
	isBusy = false;
}
//---------------------------------------------------
//ȡһ
public uint32 GetValue( uint8 vID, uint8 Command )
{
	isBusy = true;
	
	StartWrite();
	WriteByte( 0x55 );
	WriteByte( 2 );
	WriteByte( vID ); //ָֿ
	WriteByte( Command ); //ȡ
	WriteByte( 0 );
	EndWrite();
	
	repeat until DataIsOK; {}
	DataIsOK = false;
	uint32 D;
	D.0(uint8) = buffer[4];
	D.8(uint8) = buffer[5];
	D.16(uint8) = buffer[6];
	D.24(uint8) = buffer[7];
	
	isBusy = false;
	return D;
}
//----------------------------------------------------
//ǰЧֽ, ǰֽΪ. Ϊʱһֽڻᶪʧ
void SendNullData()
{
	WriteByte( 0x00 );
	WriteByte( 0x00 );
}
//----------------------------------------------------
//һֽ
void WriteByte( uint8 data )
{
	CHIP.UDR = data;
	repeat until CHIP.UCSRA.5(bit) == 1; {}
	CHIP.UCSRA.5(bit) = 1;
	//delay(); ʱҪ! ȥ֮ԽյǷд
}
//---------------------------------------------------
//485ͨʱҪʱ
void StartWrite()
{
	RDE_OUT = 1;
}
void EndWrite()
{
	//repeat 200 times; ʱȶ
	repeat 500 times; {}
	
	RDE_OUT = 0;
}






